package com.xinshujia.wxxcx.service.impl;


import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinshujia.wxxcx.mapper.WxUserMapper;
import com.xinshujia.wxxcx.pojos.entity.WxUser;
import com.xinshujia.wxxcx.pojos.info.WxUserInfo;
import com.xinshujia.wxxcx.service.WxUserService;
import com.xinshujia.wxxcx.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Cui Han
 * @Date 2024/4/27 13:54
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    private WxUserMapper wxUserMapper;


    /**
     * 登录
     * @param phone
     * @param password
     * @return
     */
    @Override
    public Result login(String phone,String password) {
        WxUser wxUser = wxUserMapper.selectOne(new LambdaQueryWrapper<WxUser>().eq(WxUser::getPhone, phone));
        if (wxUser == null){
            return Result.ok(Result.build("手机号不存在"));
        }
         else if (!wxUser.getPassword().equals(password)){
            return Result.ok(Result.build("密码错误"));
        }
        StpUtil.login(wxUser.getId());
        WxUserInfo wxUserInfo = wxUserMapper.selectUserInfo(phone);
        StpUtil.getSession().set("wxUserInfo", wxUserInfo);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return Result.ok(tokenInfo);
    }

    /**
     * 查询用户信息
     * @return
     */
    @Override
    public WxUserInfo getUserInfo() {
        WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
        return wxUserInfo;
    }
}
